﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Cherry.Agile.FileHandling;
using System.IO;

namespace Cherry.Agile.RpcService.ExtProcessor
{
    /// <summary>
    /// 服务扩展处理器，服务日志扩展插件，记录程序的错误日志。
    /// </summary>
    public class RpcTraceLoger : IServiceExtentProcessor
    {
        /// <summary>
        /// 开始处理
        /// </summary>
        /// <param name="context"></param>
        public void ProcessOnStarting(ProcessContext context)
        {
            ;
        }



        /// <summary>
        /// 结束处理
        /// </summary>
        /// <param name="context"></param>
        public void ProcessOnEnding(ProcessContext context)
        {
            ;
        }

        /// <summary>
        /// 跟踪异常信息
        /// </summary>
        /// <param name="ex">异常信息</param>
        public void ProcessTrace(Exception ex, ProcessContext context)
        {
            string msg = string.Format("{0}\r\n{1}\r\n{2}\r\n\r\n", context, ex.Message,ex.StackTrace);
            string fileName = string.Format(@"Remp\RpcSerivce\RpcErrorLog{0:yyyyMMdd}.txt", DateTime.Now);
            using (Stream stream = FileManager.OpenFile(fileName, FileMode.OpenOrCreate))
            {
                stream.Append(msg);
            }
        }

        /// <summary>
        /// 跟踪信息输出
        /// </summary>
        /// <param name="traceMessage"></param>
        public void ProcessTrace(string traceMessage, ProcessContext context)
        {
            string fileName = string.Format(@"Remp\RpcSerivce\RpcTraceLog{0:yyyyMMdd}.txt", DateTime.Now);

            string msg = string.Format("{0}\r\n{1}\r\n\r\n", context, traceMessage);
            using(Stream stream=FileManager.OpenFile(fileName, FileMode.OpenOrCreate))
            {
                stream.Append(msg);
            }
        }
    }
}
